using System;

namespace EventBehaviorTree
{
    [Serializable]
    public class Selector : Container
    {
        public override void NodeEvent(NodeMessage nodeMessage)//todo: send a stop message to the prior failure (condition) node
        {
            switch (nodeMessage)
            {
                case NodeMessage.Start:
                    Reset();
                    _subNodes[_stepCounter].NodeEvent(NodeMessage.Start);
                    return;
                case NodeMessage.Failure:
                    _stepCounter++;
                    if (_stepCounter < _subNodes.Count)
                    {
                        _subNodes[_stepCounter].NodeEvent(NodeMessage.Start);
                        return;
                    }
                    break;
                case NodeMessage.COMMAND_Stop:
                    Reset();
                    foreach (EventNode subNode in _subNodes) { subNode.NodeEvent(NodeMessage.COMMAND_Stop); }
                    break;
                default:
                    CompositeNodeEvent(nodeMessage);
                    break;
            }
            Reset();
            foreach (EventNode subNode in _subNodes) { subNode.NodeEvent(NodeMessage.COMMAND_Stop); }
        }
    }
}
